<?php
namespace app\user\controller;

use think\Controller;
use think\Request;
use think\Session;
use app\common\controller\Api;
use app\user\traits\controllers\TraitUser;

/**
 * 联系人信息管理
 */
class ContactInfo extends Api
{
    use TraitUser;
    //前置操作
    protected $beforeActionList = [
        'checkLogin',
        'checkUserInfo'
    ];
    /**
     * [checkLogin 判断是否登陆]
     * @return [type] [description]
     */
    protected function checkLogin()
    {
        if (!model('Users')->checkLogin()) {
            Session::set('redirect_url', Request::instance()->url(true));
            $this->redirect('@user/login');
            exit;
        }
    }
    //判断是否完善必填项 real_name/email
    public function checkUserInfo()
    {
        $bt1 = model('Users')->getLoginUserInfo('real_name');
        $bt2 = model('Users')->getLoginUserInfo('email');
        if (empty($bt1)||empty($bt2)) {
            $this->redirect('@userinfo/personinfo');
        }
    }   
    /**
     * [index 联系人主页]
     * @return [type] [description]
     */
    public function index()
    {   
        //获取当前登录用户之下的联系人
        $user_id=session('user.id');
        $list = model('ContactInfos')->where('user_id',$user_id)->select();
        return view('contactinfo/contactinfo',['list'=>$list]);
    }
    /**
     * [create 创建联系人信息]
     * @return [type] [description]
     */
    public function create()
    {
        return view('contactinfo/add');
    }
    /**
     * [save 保存联系人信息]
     * @return [type] [description]
     */
    public function save()
    {
        //用户id
        $user_id=session('user.id');
        $post_info['user_id']=$user_id;
        $post_info['name']=input('post.name');
        $post_info['phone']=input('post.phone');
        $post_info['email']=input('post.email');
        $post_info['type']=input('post.type');
        $post_info['type_number']=input('post.type_number');
        //halt($post_info);
        $result=model('ContactInfos')->createContactInfo($post_info);
        if ($result) {
            return $this->success(lang('添加成功'),'/contactinfo');
        }else{
            return $this->error(lang('添加失败'));
        }

    }
    /**
     * [delete 删除联系人信息]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id)
    {
        $info=model('ContactInfos')->deleteInfo($id);
        if ($info) {
            return $this->success(lang('删除成功'),'/contactinfo');
        }else{
            return $this->error(lang('删除失败'));
        }
    }
    
}

